package com.hm.oauth.web.api;

import javax.servlet.http.HttpServletRequest;

import org.apache.oltu.oauth2.common.message.types.ParameterStyle;
import org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hm.common.common.CommonUtil;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.ResultMessage;
import com.hm.common.util.CommonDef.OperationStatus;
import com.hm.oauth.def.OauthDef.Api;
import com.hm.oauth.service.AccessTokenService;
import com.hm.oauth.service.AuthorizeService;
import com.hm.oauth.service.UserService;
import com.hm.oauth.vo.UserVo;

/**
 * @author shishun.wang
 * @date 下午8:07:58 2016年8月31日
 * @version 1.0
 * @describe
 */
@Controller
@RequestMapping(Api.UserApi.USER_FINFO)
public class ApiUserInfoController {

	@Autowired
	private AccessTokenService accessTokenService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private AuthorizeService authorizeService;
	
	@RequestMapping(value = "checkAccessToken", method = RequestMethod.GET)
	public Object checkAccessToken(HttpServletRequest request) {
		try {
			OAuthAccessResourceRequest resourceRequest = new OAuthAccessResourceRequest(request, ParameterStyle.QUERY);

			String accessToken = resourceRequest.getAccessToken();
			if(CommonUtil.isEmpty(accessToken)){
				throw ServiceException.warn("AccessToken 不存在");
			}
			
			ResultMessage<UserVo> resultMessage = new ResultMessage<UserVo>();
			{
				String authorizeCode = accessTokenService.getAccssToken(accessToken);
				String userId = authorizeService.get(authorizeCode);
				
				resultMessage.setResult(userService.get(userId));
				resultMessage.setMessage(OperationStatus.QUERY_SUCCESSFUL);
			}

			return new ResponseEntity<>(resultMessage, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			ResultMessage<UserVo> resultMessage = new ResultMessage<UserVo>();
			{
				resultMessage.setMessage(e.getMessage());
			}

			return new ResponseEntity<>(resultMessage, HttpStatus.BAD_REQUEST);
		}
		
	}

	@RequestMapping(value = "notCheckAccessToken", method = RequestMethod.GET)
	public Object notCheckAccessToken(HttpServletRequest request) {
		try {
			OAuthAccessResourceRequest resourceRequest = new OAuthAccessResourceRequest(request, ParameterStyle.QUERY);

			ResultMessage<UserVo> resultMessage = new ResultMessage<UserVo>();
			{
				String accessToken = resourceRequest.getAccessToken();
				String authorizeCode = accessTokenService.getAccssToken(accessToken);
				String userId = authorizeService.get(authorizeCode);
				
				resultMessage.setResult(userService.get(userId));
				resultMessage.setMessage(OperationStatus.QUERY_SUCCESSFUL);
			}

			return new ResponseEntity<>(resultMessage, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			ResultMessage<UserVo> resultMessage = new ResultMessage<UserVo>();
			{
				resultMessage.setMessage(e.getMessage());
			}

			return new ResponseEntity<>(resultMessage, HttpStatus.BAD_REQUEST);
		}

	}
	
}
